package com.savitar.role.controller;

import com.savitar.common.vo.ResultBean;
import com.savitar.role.entity.Role;
import com.savitar.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * (Role)控制层
 *
 * @author Savitar
 * @since 2021-05-15 23:20:56
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService service;

    @GetMapping("/page")
    public ResultBean page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                           @RequestParam(value = "name", required = false) String name) {
        try {
            return ResultBean.ok(service.page(pageNum, pageSize,name));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PostMapping
    public ResultBean create(@RequestBody Role createVo) {
        try {
            return ResultBean.ok(service.create(createVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PutMapping
    public ResultBean update(@RequestBody Role updateVo) {
        try {
            return ResultBean.ok(service.update(updateVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultBean deleteById(@PathVariable String id) {
        try {
            return ResultBean.ok(service.deleteById(id));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }
}